package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.activity.util.CacheHelper;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.activity.UserRecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/activity/seckill")
public class SeckillGoodsApiController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private RabbitService rabbitService;

    //返回全部数据
    @GetMapping("findAll")
    public Result findAll(){
        List<SeckillGoods> list = seckillGoodsService.findAll();
        return Result.ok(list);
    }

    //查找秒杀商品数据
    @GetMapping("getSeckillGoods/{skuId}")
    public Result getSeckillGoods(@PathVariable Long skuId){
        SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoods(skuId);
        return Result.ok(seckillGoods);
    }

    //点击立即抢购时 生成下单码
    @GetMapping("auth/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(@PathVariable Long skuId, HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoods(skuId);
        if(seckillGoods!=null){
            //判断当前时间是否在抢购时间内
            if(DateUtil.dateCompare(seckillGoods.getStartTime(),new Date()) && DateUtil.dateCompare(new Date(),seckillGoods.getEndTime())){
                return Result.ok(MD5.encrypt(userId));
            }
        }
        return Result.fail().message("下单码生成失败");
    }

    //秒杀下单
    /**
     * 根据用户和商品ID实现秒杀下单
     * @param skuId
     * @return
     */
    @PostMapping("auth/seckillOrder/{skuId}")
    public Result seckillOrder(@PathVariable Long skuId,HttpServletRequest request){
        // url: this.api_name + '/auth/seckillOrder/' + skuId + '?skuIdStr=' + skuIdStr,
        //1、校验下单码
        String userId = AuthContextHolder.getUserId(request);
        //从请求中获取下单码
        String skuIdStr = request.getParameter("skuIdStr");
        if(!skuIdStr.equals(MD5.encrypt(userId))){
            return Result.fail().message("下单码校验失败");
        }
        //2、校验状态位state   skuId:1 可以买    skuId:0 不可以买
        String state = (String) CacheHelper.get(skuId.toString());
        if(StringUtils.isEmpty(state)){
            return Result.fail().message("非法请求");
        }else if ("0".equals(state)){
            return Result.fail().message("该商品已售罄");
        }else {
            UserRecode userRecode = new UserRecode();
            userRecode.setSkuId(skuId);
            userRecode.setUserId(userId);

            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_SECKILL_USER,MqConst.ROUTING_SECKILL_USER,userRecode);
            return Result.ok();
        }
    }
}
